﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Enums;

namespace BuildPlannerLib.SimEvents
{
    // Derived class from Event for a worker finishing mining at a mineral patch
    public class FinishMineralMining : SimEvent
    {
        // Field for the base at which mining is taking place
        public Base AtBase;

        // Field for the type of patch being mined
        public MineralFlags Flags;

        // Constructor with parameters for the parent build, base, mineral flags, and time
        public FinishMineralMining(Build build, Base b, MineralFlags flags, int time)
        {
            Build = build;
            AtBase = b;
            Flags = flags;
            Time = time;
        }

        // Adds an event for returning the minerals, checks for workers waiting to mine and has them mine the patch, otherwise the patch is set to Idle
        public override void Execute()
        {
            int newTime = Time;
            newTime += (Flags == MineralFlags.ClosePatch) ? 186 : 238;
            int yield = (Flags == MineralFlags.GoldPatch) ? 7 : 5;
            Build.EventList.Add(new ReturnMinerals(Build, AtBase, yield, newTime));

            newTime = Time;

            if (Flags == MineralFlags.ClosePatch)
            {
                if (AtBase.MineralWorkerStatus[1] > 0)
                {
                    AtBase.MineralWorkerStatus[1]--;
                    newTime += 294;
                    Build.EventList.Add(new FinishMineralMining(Build, AtBase, Flags, newTime));

                    // Remove the next ReachFarMinerals event for workers at this base from the list, since the CloseMineralWorker will be used to mine this patch.
                    SimEvent rem = Build.EventList.OfType<ReachFarMinerals>().First<ReachFarMinerals>(obj => obj.GetBase() == AtBase);
                    Build.EventList.Remove(rem);
                }
                else if (AtBase.MineralWorkerStatus[2] > 0)
                {
                    AtBase.MineralWorkerStatus[2]--;
                    newTime += 294;
                    Build.EventList.Add(new FinishMineralMining(Build, AtBase, Flags, newTime));
                }
                else
                {
                    if (Flags == MineralFlags.GoldPatch)
                    {
                        AtBase.IdleMineralPatches[3]++;
                    }
                    else
                    {
                        AtBase.IdleMineralPatches[2]++;
                    }
                }
            }
            else
            {
                if (AtBase.MineralWorkerStatus[2] > 0)
                {
                    AtBase.MineralWorkerStatus[2]--;
                    newTime += 294;
                    Build.EventList.Add(new FinishMineralMining(Build, AtBase, Flags, newTime));
                }
                else
                {
                    if (Flags == MineralFlags.GoldPatch)
                    {
                        AtBase.IdleMineralPatches[1]++;
                    }
                    else
                    {
                        AtBase.IdleMineralPatches[0]++;
                    }
                }
            }
        }
    }
}
